﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PhoneDBModel;

public partial class Admin_SetCosts : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Visible = false;
        PhoneDBEntities dbe = new PhoneDBEntities();
        if (Page.IsPostBack == false)
        {
            
            Price cr = dbe.Prices.Where(x => x.Code == ddlCountry.SelectedItem.Value).FirstOrDefault();
            txtCost.Text = cr.CPM.ToString();
        }

        var costs = dbe.Prices;
        gvCosturi.DataSource = costs;
        gvCosturi.DataBind();
    }
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        PhoneDBEntities dbe = new PhoneDBEntities();
        Price cr = dbe.Prices.Where(x => x.Code == ddlCountry.SelectedItem.Value).FirstOrDefault();
        txtCost.Text = cr.CPM.ToString();
        //foreach (ListItem item in ddlCountry.Items)
        //{
        //    Price pr = new Price();
        //    pr.Country = item.Text;
        //    pr.Code = item.Value;
        //    pr.CPM = 0.01m;
        //    dbe.Prices.AddObject(pr);
        //    dbe.SaveChanges();
        //}    
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        PhoneDBEntities dbe = new PhoneDBEntities();
        Price cr = dbe.Prices.Where(x => x.Code == ddlCountry.SelectedItem.Value).FirstOrDefault();
        cr.CPM = decimal.Parse(txtCost.Text);
        dbe.SaveChanges();
        lblMessage.Visible = true;
    }
}